package org.samith.web.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.samith.dao.ServiceTypeDao;
import org.samith.dao.SubscribedServiceDao;
import org.samith.dao.SubscriberDao;
import org.samith.dao.TransactionDao;
import org.samith.entity.ServiceType;
import org.samith.entity.SubscribedService;
import org.samith.entity.Subscriber;
import org.samith.web.helper.AuthenticatedUserSession;

@SuppressWarnings("serial")
public class ServiceActivationWSClientServlet extends HttpServlet {

	@EJB
	ServiceTypeDao serviceTypeDao;
	@EJB
	SubscriberDao subscriberDao;
	@EJB
	SubscribedServiceDao subscribedServiceDao;
	@EJB
	TransactionDao transactionDao;
	@Resource
	UserTransaction tx;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ServletContext context = getServletConfig().getServletContext();
		HttpSession session = request.getSession();
		AuthenticatedUserSession userSession = (AuthenticatedUserSession) session
				.getAttribute("authenticatedUser");
		// userSession cannot be null because this page is
		// viewed by logged in users only.
		Long userId = userSession.getUserId();
		Subscriber subscriber = subscriberDao.findByUserId(userId);
		Collection<SubscribedService> subscribedServices = subscribedServiceDao
				.findSubscribedServicesBySubscriberId(subscriber.getId());
		Collection<ServiceType> services = serviceTypeDao.list();
		request.setAttribute("subscribedServices", subscribedServices);
		request.setAttribute("services", services);

		context.getRequestDispatcher("/Services/doServiceActivation.jsp")
				.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		AuthenticatedUserSession userSession = (AuthenticatedUserSession) session
				.getAttribute("authenticatedUser");
		int userId = userSession.getUserId().intValue();
		String serviceToBeActivated = request.getParameter("serviceId");

		IServiceActivation client = getClientWSProxy();
		boolean serviceActivated = client.activateUserForService(userId,
				Integer.valueOf(serviceToBeActivated));
		System.out.println("========= Web service service activation status "+serviceActivated);
		response.sendRedirect("/ejbApp/WS/client/activateServices");
	}

	private IServiceActivation getClientWSProxy() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		factory.setServiceClass(IServiceActivation.class);
		factory.setAddress(getServletConfig().getInitParameter(
				"activationServiceWSDLPath"));
		IServiceActivation client = (IServiceActivation) factory.create();
		return client;
	}
}

/**
 * Use this utility method to convert Java to JSon
 * 
 * Gson gson = new Gson(); String jsonServices = gson.toJson(services);
 * response.setContentType("application/json"); PrintWriter out =
 * response.getWriter(); out.print(jsonServices); out.flush();
 * 
 **/
